package cn.edu.cqu.bysj.wm.gui;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.def.ConstDef.AlgorithmType;
import cn.edu.cqu.bysj.wm.gui.component.AlgorithmTabPane;
import cn.edu.cqu.bysj.wm.gui.component.JDTotalTabPane;
import cn.edu.cqu.bysj.wm.gui.model.AlgorithmConfig;

public class JDConfigUI extends ConfigUi {
	private ButtonGroup mainAlgorithmGroup;

	/**
	 * key node config dialog
	 * 
	 * @param parent
	 * @param isModel
	 * @param configure
	 */
	public JDConfigUI(MainFrame parent, boolean isModel,
			AlgorithmConfig configure) {
		super(parent, isModel);
		this.config = configure;

		JRadioButton skylineRadioButton = new JRadioButton(
				ConstDef.SKYLINE_ALGORITHM_STRING);
		skylineRadioButton.setActionCommand(ConstDef.SKYLINE_ALGORITHM_STRING);
		JRadioButton faginRadioButton = new JRadioButton(
				ConstDef.FAGIN_ALGORITHM_STRING);
		faginRadioButton.setSelected(true);
		faginRadioButton.setActionCommand(ConstDef.FAGIN_ALGORITHM_STRING);

		mainAlgorithmGroup = new ButtonGroup();

		mainAlgorithmGroup.add(skylineRadioButton);

		mainAlgorithmGroup.add(faginRadioButton);

		JLabel label = new JLabel("算法选择");
		GroupLayout groupLayout = new GroupLayout(childContent);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				label))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				skylineRadioButton))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				faginRadioButton)))
										.addContainerGap(84, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap()
						.addComponent(label).addGap(18)
						.addComponent(faginRadioButton).addGap(18)
						.addComponent(skylineRadioButton)
						.addContainerGap(120, Short.MAX_VALUE)));

		childContent.setLayout(groupLayout);
		this.initExistedConfig();
	}

	private void initExistedConfig() {
		super.initExistedConfig(config.getSuperConfig());
	}

	@Override
	protected AlgorithmTabPane saveConfig() {

		ButtonModel bm = mainAlgorithmGroup.getSelection();
		if (bm == null) {
			JOptionPane.showMessageDialog(this, "请选择算法");
			return null;
		}
		config.setAlgorithmType(AlgorithmType.valueOfType(bm
				.getActionCommand()));
		return new JDTotalTabPane(ConstDef.JD_TYPE_STRING
				+ config.getTitle(), config);
	}

	@Override
	protected void doAlgorithm() {
		AlgorithmTabPane panel = saveConfig();
		if (panel == null) {
			return;
		}
		dispose();
		superParent.addTab(panel.getTitle(), panel);
	}

}
